<?php
/** Yz_SQL_Join
 *
 */
use Yz_SQL_DMLStatement as SQL;

class Yz_SQL_Join
    extends Yz_Class
{

    protected $__dict__ = array(
        'type' => '',
        'table' => '',
        'alias' => '',
        'how' => '',
        'conditions' => '',
    );

    protected function __construct($type, $table, $alias)
    {
        $this->__dict__['type']  = $type;
        $this->__dict__['table'] = $table;
        $this->__dict__['alias'] = $alias;
    }

    public static function leftJoin($table, $alias=null)
    {
        return new static(SQL::LEFT_JOIN, $table, $alias);
    }

    public static function innerJoin($table, $alias=null)
    {
        return new static(SQL::INNER_JOIN, $table, $alias);
    }

    public static function commaJoin($table, $alias=null)
    {
        return new static(SQL::COMMA_JOIN, $table, $alias);
    }


    public function joinOn($conds)
    {
        if (is_string($conds)) {
            if ("" === $conds)
                throw ValueError::_new_();
        } else {
            $list = new Yz_Array($conds);
            if ($list->isEmpty())
                throw ValueError::_new_();

            $conds = $list->join(" AND ");
        }
        $this->__dict__['how'] = SQL::JOIN_ON;
        $this->__dict__['conditions'] = $conds;
        return $this;
    }

    public function joinUsing($fields)
    {
        assert('is_string($fields) and $fields !== ""');
        $this->__dict__['how'] = SQL::JOIN_USING;
        $this->__dict__['conditions'] = $fields;
        return $this;
    }

} // class

